package _8_Gorm框架老郭

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// 原生查询 SQL 和 Scan
func main() {
	dsn := "root:123456@tcp(127.0.0.1:3306)/golang_db?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect darabase")
	}

	type Result struct {
		ID   int
		Name string
		Age  int
	}

	var result Result
	db.Raw("SELECT id, name, age FROM users WHERE name = ?", 3).Scan(&result)

	db.Raw("SELECT id, name, age FROM users WHERE name = ?", 3).Scan(&result)

	var age int
	db.Raw("SELECT SUM(age) FROM users WHERE role = ?", "admin").Scan(&age)

	var users []User
	db.Raw("UPDATE users SET name = ? WHERE age = ? RETURNING id, name", "jinzhu", 20).Scan(&users)

}
